package edu.ccut.saturn.studio.logic.parseutil;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.saturn.ErrorInfo;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnArg;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnValidate;


public class ParseValidate {
	/**
	 * 封装saturn_validatesNode节点
	 * 
	 * @param validatesNode
	 * @return
	 */
	public static   SaturnValidate setvalidate(Document lidatedosaturn_Validatedomm) {
		Node rootnode = lidatedosaturn_Validatedomm.getDocumentElement();
		String rootname=rootnode.getNodeName();
		if(rootname.equals("saturn-validates")){
			NodeList validateNodeList = rootnode.getChildNodes();
			for (int i = 0; i < validateNodeList.getLength(); i++) {
				Node validateNode = validateNodeList.item(i);
				String nodeName = validateNode.getNodeName();
				if ("validate".equals(nodeName)) {
					NodeList newNodeList = validateNode.getChildNodes();
					SaturnValidate validateVO = new SaturnValidate();
					for (int j = 0; j < newNodeList.getLength(); j++) {
						Node node = (Node) newNodeList.item(j);
						String Name = node.getNodeName();
						if (Name.equals("key") || "name".equals(Name)
								|| "class".equals(Name)
								|| "description".equals(Name)
								|| "args".equals(Name)|| "js-method-name".equals(Name)|| "custom-js-method".equals(Name)||"errorInfos".equals(Name)||"eventInfos".equals(Name)||"validateType".equals(Name)) {
							
							if ("key".equals(Name)) {
								validateVO.setKey(node.getTextContent());
							}
							if ("name".equals(Name)) {
								validateVO.setName(node.getTextContent());

							}
							if ("class".equals(Name)) {
								validateVO.setValidateClass(node.getTextContent());

							}
							if ("description".equals(Name)) {
								validateVO.setDescription(node.getTextContent());

							}
							if ("js-method-name".equals(Name)) {
								validateVO.setJsMethodName(node.getTextContent());

							}
							if ("custom-js-method".equals(Name)) {
								validateVO.setCustomJsMethod(node.getTextContent());

							}
							if("validateType".equals(Name)){
								validateVO.setValidateType(node.getTextContent());
							}
							if("eventInfos".equals(Name)){
								validateVO.setEventInfos(getEventInfoList(node));
							}
							if("errorInfos".equals(Name)){
								validateVO.setErrorInfos(getErrorInfoList(node));
							}
							if ("args".equals(Name)) {
								NodeList argNodeList = node.getChildNodes();
								List<SaturnArg> argList = new ArrayList<SaturnArg>();
								int fludid=0;
								for (int k = 0; k < argNodeList.getLength(); k++) {
									Node argnode = (Node) argNodeList.item(k);
									if (argnode.getNodeName().equals("arg")) {
										SaturnArg argsVo = new SaturnArg();
									
										argsVo.setId(""+(++fludid));
										argsVo.setArg(argnode.getTextContent());
										argList.add(argsVo);
									}
								}
								validateVO.setArgs(argList);

							}
							
						}
						
					}
					return validateVO;
				}
		    }
	
		}
	return null;
		
	}
	
	private static List<ErrorInfo> getErrorInfoList(Node errorInfosN) {
		List<ErrorInfo> errorInfos = new ArrayList<ErrorInfo>();
		if (errorInfosN != null) {
			List<Node> argNodes = getChildNodes(errorInfosN,
					"errorInfo");
			
			for (Node errorInfo : argNodes) {
				errorInfos.add(getErrorInfo(errorInfo));
			}
			
		}
		return errorInfos;
	}
	
	private static ErrorInfo getErrorInfo(Node errorInfoN) {
		
		if(errorInfoN instanceof Element){
			ErrorInfo errorInfo = new ErrorInfo();
			errorInfo.setKey(((Element)errorInfoN).getAttribute("key"));
			errorInfo.setValue(errorInfoN.getFirstChild().getNodeValue().trim());
			return errorInfo;
		}
		return null;
	}
	
	private static List<String> getEventInfoList(Node eventInfosN) {
		List<String> eventInfos = new ArrayList<String>();
		if (eventInfosN != null) {
			List<Node> eventInfoNs = getChildNodes(eventInfosN,
			"eventInfo");
			
			for (Node errorInfoN : eventInfoNs) {
				eventInfos.add(errorInfoN.getFirstChild().getNodeValue());
			}
			
		}
		return eventInfos;
	}
	
	private static Node getChildNode(Node parentNode, String nodeKey) {
		NodeList children = parentNode.getChildNodes();

		for (int i = 0; i < children.getLength(); ++i) {
			Node child = children.item(i);

			if (child.getNodeName().equals(nodeKey)) {
				return child;
			}
		}

		return null;
	}
	
	private static List<Node> getChildNodes(Node parentNode, String nodeKey) {
		NodeList children = parentNode.getChildNodes();

		List<Node> childNodes = new ArrayList<Node>();

		for (int i = 0; i < children.getLength(); ++i) {
			Node child = children.item(i);

			if (child.getNodeName().equals(nodeKey)) {
				childNodes.add(child);
			}
		}

		return childNodes;
	}

}
